package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.baiyang.server.tools.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.Role;
import com.baiyang.server.service.system.RoleService;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/roleOperation")
@Jurisdiction(value = "roleOperation")
public class RoleCon {
    @Autowired
    private RoleService roleService = null;

    /**
     * 根绝条件分页获取数据
     *
     * @return
     */
    @Jurisdiction(value = "getPage")
    @RequestMapping(value = "/getPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<Role> getPage(@RequestBody JSONObject object) {
        Letter<Role> letter = new Letter<Role>();
        try {
            PageEntity<Role> result = roleService.getPage(object);
            letter.setList(result.getDatas());
            letter.setTotal(result.getCount());
            letter.setDetail(result.getResult());
            letter.setResult("SUCCESS");
            letter.setDetail("数据获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据获取失败！");
        }
        return letter;
    }


    /**
     * 根据条件获取一个用户信息
     *
     * @param role
     * @return
     */
    @Jurisdiction(value = "selectOne")
    @RequestMapping(value = "/selectOne", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<Role> selectOne(@RequestBody Role role) {
        Letter<Role> letter = new Letter<Role>();
        try {
            Role obj = roleService.selectOne(role);
            letter.setData(obj);
            letter.setResult("SUCCESS");
            letter.setDetail("数据获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据获取失败！");
        }
        return letter;
    }

    @Jurisdiction(value = "selectAll")
    @RequestMapping(value = "/selectAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<Role> selectAll() {
        Letter<Role> letter = new Letter<Role>();
        try {
            Role role = new Role();
            role.setIsFlag("1");
            List<Role> obj = roleService.selectList(role);
            letter.setList(obj);
            letter.setResult("SUCCESS");
            letter.setDetail("数据获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据获取失败！");
        }
        return letter;
    }

    /**
     * 插入数据
     *
     * @param role
     * @return
     */
    @Jurisdiction(value = "insert")
    @RequestMapping(value = "/insert", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> insert(@RequestBody Role role) {
        Letter<String> letter = new Letter<String>();
        Map<String, String> sub = SubjectNotNull.subject(role);
        if (sub.size() != 0) {
            letter.setResult("FAIL");
            letter.setDetail("必填信息不能为空！");
            return letter;
        }
        try {
            role.setId(DataTool.createUUID());
            roleService.insert(role);
            letter.setDetail("数据插入成功");
            letter.setResult("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据插入失败！");
        }
        return letter;
    }

    /**
     * 批量插入数据
     *
     * @param roles
     * @return
     */
    @Jurisdiction(value = "insertBatch")
    @RequestMapping(value = "/insertBatch", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> insertBatch(@RequestBody List<Role> roles) {
        Letter<String> letter = new Letter<String>();
        try {
            roleService.insertBatch(roles, letter);
            letter.setDetail("数据插入成功");
            letter.setResult("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据插入失败！");
        }
        return letter;
    }

    /**
     * 根绝条件进行删除
     *
     * @param role
     * @return
     */
    @Jurisdiction(value = "delete")
    @RequestMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> delete(@RequestBody Role role) {
        Letter<String> letter = new Letter<String>();
        if (SubjectNotNull.subjectIsNull(role)) {
            letter.setResult("FAIL");
            letter.setDetail("信息不能为空！");
            return letter;
        }
        try {
            roleService.delete(role);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除成功！");
        }
        return letter;
    }

    /**
     * 根绝条件进行删除
     *
     * @param roles
     * @return
     */
    @Jurisdiction(value = "deletes")
    @RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> deletes(@RequestBody List<Role> roles) {
        Letter<String> letter = new Letter<String>();
        if (DataTool.isBlank(roles)) {
            letter.setResult("FAIL");
            letter.setDetail("请至少选中一行！");
            return letter;
        }
        try {
            roleService.deletes(roles);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除成功！");
        }
        return letter;
    }


    /**
     * 根绝id更新
     *
     * @param role
     * @return
     */
    @Jurisdiction(value = "updateById")
    @RequestMapping(value = "/updateById", produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> updateById(@RequestBody Role role) {
        Letter<String> letter = new Letter<String>();
        if (role.getId().equals("")) {
            letter.setResult("FAIL");
            letter.setDetail("id不能为空！");
            return letter;
        }
        try {
            roleService.updateById(role);
            letter.setDetail("数据修改成功");
            letter.setResult("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据修改成功！");
        }
        return letter;
    }

    /**
     * @apiNote 初始化角色信息，创建超级管理员角色
     * @author XVX
     */
    @Jurisdiction(value = "initRole")
    @RequestMapping(value = "/initRole", produces = MediaType.APPLICATION_JSON_VALUE)
    public void initRole() {
        Role role = new Role();
        role.setId(DataTool.createUUID());
        role.setIsFlag("1");
        role.setRoleName("超级系统管理员");
        role.setCreateBy("auto_baiyang");
        roleService.insert(role);
    }
}
